package com.cgc999.host.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.cgc999.host.R;
import com.cgc999.host.api.PromoteService;
import com.cgc999.host.vo.CgcPromoteArticle;
import com.cgc999.host.vo.ResponseInfo;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @CreateDate: 2020/1/10 11:03
 * @Description: java类作用描述
 * @Author: lgh
 */
public class ArticleEditActivity extends AppCompatActivity {

    Retrofit retrofit=null;
    EditText editTitle,editDesc;
    Button btnSubmit;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_promote_edit);
        editTitle=findViewById(R.id.edit_title);
        editDesc=findViewById(R.id.edit_desc);
        btnSubmit=findViewById(R.id.btn_submit);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });

    }

    private void initData(){
        String title=editTitle.getText().toString();
        String desc=editTitle.getText().toString();
        retrofit=new Retrofit.Builder().baseUrl("http://192.168.10.121:18011")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        PromoteService promoteService=retrofit.create(PromoteService.class);
        String json="{   \n" +
                "\t\"promote_id\": \"apUser180839080003\",\n" +
                "\t\"title\":\" "+title+"\",\n" +
                "\t\"content\":\""+desc+"\",\n" +
                "\t\"summary\":\"文章摘要\",\n" +
                "\t\"author\":\"lgh\",\n" +
                "\t\"cover_url\":\"https://img.zcool.cn/community/01a51b5a38832aa80120ba38e6521d.png@1280w_1l_2o_100sh.png\",\n" +
                "\t\"article_url\":\"https://www.baidu.com\"\n" +
                "}";
        RequestBody requestBody=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        promoteService.addArticle(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseInfo<CgcPromoteArticle>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseInfo<CgcPromoteArticle> cgcPromoteArticleResponseInfo) {
                        Toast.makeText(ArticleEditActivity.this,"已提交",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


}
